<?php
/**
 * CaptchaValidate.php
 * @author zohnの梦
 * @email  1171672036@qq.com
 * @date   2021/1/8
 */

namespace app\api\validate;


use app\common\service\ImageCaptchaService;
use think\Validate;

class CaptchaValidate extends Validate
{
    protected $rule = [
        'image_captcha_id'   => ['require'],
        'image_captcha_code' => ['require', 'checkImageCaptcha'],
    ];

    protected $message = [
        'image_captcha_id.require'   => '验证码 id 不能为空',
        'image_captcha_code.require' => '验证码不能为空',
    ];

    protected $scene = [
        'login' => ['image_captcha_id', 'image_captcha_code'],
    ];

    // 自定义验证规则：验证码验证
    protected function checkImageCaptcha($value, $rule, $data = [])
    {
        $id   = $data['image_captcha_id'];
        $code = $data['image_captcha_code'];

        $ImageCaptchaService = new ImageCaptchaService();
        $check_imageCaptcha  = $ImageCaptchaService->check($id, $code);
        if (empty($check_imageCaptcha)) {
            return '验证码错误';
        }

        return true;
    }
}